package com.ctyon.watchmanager.ui.fragment;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ctyon.common.config.glid.GlideApp;
import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.SPUtils;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.config.ImageBean;
import com.ctyon.watchmanager.databinding.FragmentMineBinding;
import com.ctyon.watchmanager.model.net.entity.Manager;
import com.ctyon.watchmanager.present.MinePresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.previewlibrary.GPreviewBuilder;
import com.umeng.analytics.MobclickAgent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

import static com.ctyon.common.container.ARouterPath.ROUTER_ACTIVITY_CONTAINER;
import static com.ctyon.common.container.ARouterPath.ROUTER_ACTIVITY_LOGIN;
import static com.ctyon.common.util.SPUtils.IS_LOGIN;
import static com.ctyon.watchmanager.ui.activity.ContainerActivity.TAB_INDEX_PARAM;

/**
 * CreateDate：19-6-25 on 上午9:13
 * Describe:
 * Coder: lee
 */
public class MineFragment extends BaseFragment {

    public static final String TAG = "MineFragment";

    private FragmentMineBinding mBinding;
    private AlertDialog.Builder builder;

    public static MineFragment newInstance() {
        final MineFragment fragment = new MineFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_mine, container, false);

        mBinding.setMinePresenter(minePresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mBinding.setCover(BasicApp.getInstance().getManager().getImg());
        mBinding.setNick(BasicApp.getInstance().getManager().getNickName());
    }

    private final MinePresenter minePresenter = new MinePresenter() {
        @Override
        public void onCoverImageClick(View view) {
            if (DataUtil.isEmptyAnd(mBinding.getCover())) {
                return;
            }

            ImageBean imageBean = new ImageBean(mBinding.getCover());
            Rect bounds = new Rect();
            view.getGlobalVisibleRect(bounds);
            imageBean.setBounds(bounds);

            GPreviewBuilder.from(getActivity())
                    .setSingleData(imageBean)
                    .setSingleShowType(false)
                    .setCurrentIndex(0)
                    .setType(GPreviewBuilder.IndicatorType.Dot)
                    .start();
        }

        @Override
        public void onSettingClick() {

        }

        @Override
        public void onAboutClick() {
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, AboutFragment.TAG)
                    .navigation();
        }

        @Override
        public void onLoginOutClick() {
            showDialog();
        }

        @Override
        public void onEditClick() {
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, MineInfoFragment.TAG)
                    .navigation();
        }
    };

    private void showDialog() {
        if (null == builder) {
            builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog);
            builder.setTitle("退出登陆")
                    .setMessage("确定退出登陆吗?")
                    .setPositiveButton("确定", (dialog, which) -> {
                        //登出
                        MobclickAgent.onProfileSignOff();
                        SPUtils.getInstance().put(IS_LOGIN, false);
                        BasicApp.getInstance().getBeanContainer().clear();
                        BasicApp.getInstance().setManager(null);
                        ARouter.getInstance().build(ROUTER_ACTIVITY_LOGIN).navigation();
                        getActivity().finish();
                    });
        }
        builder.show();
    }


}
